﻿using Microsoft.Extensions.Caching.Memory;

namespace LowCoding.Common
{
    public class DataCache
    {
        public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());

        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            return _memoryCache.Get(CacheKey);
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            if (objObject == null) return;
            _memoryCache.Set(CacheKey, objObject);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="absoluteExpiration"></param>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration)
        {
            _memoryCache.Set(CacheKey, objObject, absoluteExpiration);
        }

        public static void SetCache(string CacheKey, object objObject, TimeSpan slidingExpiration)
        {
            if (objObject == null) return;
            _memoryCache.Set(CacheKey, objObject, new MemoryCacheEntryOptions()
                    .SetSlidingExpiration(slidingExpiration));
        }

        public static void RemoveCache(string CacheKey)
        {
            _memoryCache.Remove(CacheKey);
        }
    }
}